class_name Goal
extends Node2D

@onready var back_net_area: Area2D = %BackNetArea
@onready var targets: Node2D = %Targets
@onready var scoring_area: Area2D = %ScoringArea

var country: String

func _ready() -> void:
	back_net_area.body_entered.connect(on_ball_enter_back_net.bind())
	scoring_area.body_entered.connect(on_ball_enter_score_area.bind())
	
func initialize(context_country: String) -> void:
	country = context_country
	
func on_ball_enter_back_net(ball: Ball) -> void:
	ball.stop()
	
func on_ball_enter_score_area(_ball: Ball) -> void:
	SoundPlayer.play(SoundPlayer.Sound.WHISTLE)
	GameEvent.team_scored(country)
	
func get_random_target_position() -> Vector2:
	return targets.get_child(randi_range(0, targets.get_child_count() - 1)).global_position
	
func get_center_target_position() -> Vector2:
	return targets.get_child(int(targets.get_child_count() * 0.5)).global_position
	
	
func get_top_target_position() -> Vector2:
	return targets.get_child(0).global_position
	
func get_bottom_target_position() -> Vector2:
	return targets.get_child(targets.get_child_count() - 1).global_position
	
func get_scoring_area() -> Area2D:
	return scoring_area
